<template>
    <div class="goodsinfo-container">
        <transition  
        @before-enter="beforeEnter"
        @enter="enter"
        @after-enter="afterEnter">
        <div class="ball" v-show="ballFlag" ref="ball"></div>
        </transition>
        <!-- 商品轮播图区域 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                     <swiper :LunbotuList="lunbotu" ></swiper>
                </div>
            </div>
        </div>
    
        <!-- 商品购买信息区域 -->
        <div class="mui-card">
				<div class="mui-card-header">商品选择</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<p class="price">
                            市场价：<del>￥{{goodsinfo.market_price}}</del>&nbsp; &nbsp;  销售价 ：<span class="now_price">￥{{goodsinfo.sell_price}}</span>
                        </p>
                        
                       <p class="buy_count">
                           购买数量：<numbox :max="goodsinfo.stock_quantity" @getCount="getSelectedCount"></numbox>
                       </p> 
                        <p>
                            <mt-button type="primary" size="small" >立即购买</mt-button>
                            <mt-button type="danger" size="small"  @click="addToShopCar">加入购物车</mt-button>
                        </p>
					</div>
				</div>
		</div>

        <!-- 商品详情区域 -->
        <div class="mui-card">
				<div class="mui-card-header">商品详情</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
                         <p>货物货号：{{goodsinfo.goods_no}}</p>
                         <p>商品库存：{{goodsinfo.stock_quantity}}</p>
                         <p>上架时间：{{goodsinfo.add_time}}</p>
					</div>
				</div>
				<div class="mui-card-footer">
                      <mt-button type="primary" size="large"  @click="goDesc(goodsinfo.id)" plain>图文介绍</mt-button>
                      <br>
                      <mt-button type="danger" size="large"  @click="goComment(goodsinfo.id)"  plain>商品评论</mt-button>
                </div>
		</div>
    </div>
</template>

<script>
import {Toast} from 'mint-ui'
import numbox from './goodsinfo_number.vue'

import swiper from '../subcomponent/swiper.vue'

export default {
    data(){
        return{
            id:this.$route.params.id,
            lunbotu:[],
            goodsinfo:{},
            ballFlag:false  ,//控制小球的隐藏和显示
            selectCount:1
        }
    },
    created(){
        this.getLunbotu();
        this.getGoodsinfo();
    },
    methods:{
        getLunbotu(){
            this.$http.get("api/getthumimages/"+this.id).then(
                result =>{
                    if(result.body.status ==0){
                        //在渲染轮播图之前  ，先要定义item.img  因为轮播图组件不认识 item.src
                        result.body.message.forEach(item =>{
                            item.img = item.src;
                        });
                        this.lunbotu  = result.body.message
                    }
                    else{
                        Toast("加载商品轮播图失败.")
                    }
                }
            )
        },
        //获取商品详细信息
        getGoodsinfo(){
             this.$http.get("api/goods/getinfo/"+this.id).then(
                 result =>{
                    if(result.body.status == 0){
                      this.goodsinfo =  result.body.message[0]
                    }
                    else{
                        Toast("获取商品信息失败...")
                    }
                 }
             )
        },
        goDesc(id){
            this.$router.push({ name:"goodsdesc",params:{ id } })
        },
        goComment(id){
            this.$router.push({ name:"goodscomment",params:{ id } })
        },
        addToShopCar(){
            this.ballFlag = !this.ballFlag
            // 构建一个购物车对象
            var goodsinfo = {id:this.id,count:this.selectCount,price:this.goodsinfo.sell_price,selected:true}

            // 调用store实例中的方法
            this.$store.commit("addToCat",goodsinfo)

        },
        beforeEnter(el){
            el.style.transform ="translate(0,0)"
        },
        enter(el,done){
            el.offsetWidth
             // 获取小球在页面中的位置
             const ballPosition = this.$refs.ball.getBoundingClientRect();
            
            // 获取徽标在页面中的位置
            const badgePosition = document.getElementById("badge").getBoundingClientRect();

            const xDist = badgePosition.left - ballPosition.left
            const yDist = badgePosition.top - ballPosition.top
            el.style.transform =`translate(${xDist}px,${yDist}px)`
            el.style.transition = "all 1s cubic-bezier(.12,.46,.83,.67)"
            done()
        },
        afterEnter(el){
            this.ballFlag = !this.ballFlag
        },
        getSelectedCount(count){
            this.selectCount = count
            console.log("父组件拿到的值："+ this.selectCount)
        }
        
    },
    components:{
        swiper,
        numbox
    }
}
</script>

<style lang="scss" scoped>
.goodsinfo-container{
    background-color: #eee;
    overflow: hidden;
}

.goodsinfo-container  .now_price{
    font-size:16px;
    color:red;
    font-weight: bold;
}

.goodsinfo-container .buy_count{
       /* display: inline-block; */
}
.mui-card-footer {
    display: block;
}
.ball{
    width: 15px;
    height: 15px;
    background-color:red;
    border-radius: 50%;
    position: absolute;
    z-index:99;
    top:390px;
    left:146px;
}
</style>